<template>
  <div>
    <!-- 搜索表单 -->
    <div class="nav">
      <div class="right">
        <el-form
          :inline="true"
          ref="searchForm"
          :model="search"
          class="demo-form-inline"
        >
          <el-form-item label="门店名" prop="name">
            <el-input v-model="search.name" placeholder="门店名"></el-input>
          </el-form-item>
          <el-form-item label="门店地址" prop="address">
            <el-input
              v-model="search.address"
              placeholder="门店地址"
            ></el-input>
          </el-form-item>
          <el-form-item label="业态" prop="type">
            <el-input v-model="search.type" placeholder="业态"></el-input>
          </el-form-item>
          <el-form-item label="管理区域" prop="type">
            <el-input v-model="search.area" placeholder="管理区域"></el-input>
          </el-form-item>
         
        </el-form>
      </div>
      <!-- 重置查询 -->
      <div class="left">
        <el-link type="primary" @click="resetForm('searchForm')"
          >重置筛选条件</el-link
        >
        <el-button
          type="primary"
          @click="
            search.current = 1;
            getShop();
          "
          icon="el-icon-search"
          >查询</el-button
        >
      </div>
    </div>
    <!-- 增加门店 -->
    <div class="add">
      <el-button type="primary" icon="el-icon-plus" @click="visible = true"
        >新增门店</el-button
      >
    </div>
    <div class="content">
      <!-- 改变表头颜色 :header-cell-style="{background:'#f0f0f0'}" -->
      <el-table
        :header-cell-style="{ background: '#eaedf4' }"
        ref="multipleTable"
        :data="shop"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <!-- @selection-change="handleSelectionChange" -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="店名"> </el-table-column>
        <el-table-column prop="type" label="业态"> </el-table-column>
        <el-table-column prop="address" label="地址"> </el-table-column>
        <el-table-column prop="area" label="管理区域"> </el-table-column>
        <el-table-column prop="oparate_area" label="所在区"> </el-table-column>
        <el-table-column prop="open_time" label="开始营业时间">
        </el-table-column>
        <el-table-column prop="open_time" label="结束营业时间">
        </el-table-column>

        <el-table-column prop="" label="是否上线">
          <el-switch v-model="shop.delevery_type"> </el-switch>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              icon="el-icon-edit-outline"
              style="color: #4c93ff"
              @click="$router.push('/admin/shop/editShop/' + scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              icon="el-icon-delete"
              style="color: #ff8e8d"
              @click="delShop(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pagnation.current"
      :page-sizes="[5, 10, 20]"
      :page-size="pagnation.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagnation.total"
    >
    </el-pagination>
    <AddShop :visible.sync="visible"></AddShop>
  </div>
</template>
<script>
import { GetShop, DelShop } from "@/api/shop";
import AddShop from "@/components/AddShop.vue";
import bus from "@/utils/bus.js";
export default {
  components: {
    AddShop,
  },
  // activated(){
  //   this.getShop();
  // },
  data() {
    return {
      visible: false,
      shop: {
        order: "desc",
      },
      pagnation: {},
      // 搜索条件
      search: {
        name: "",
        // 当前页
        current: 1,
        // 每页多少条
        size: 10,
        // 降序
        order: "desc",
        address: "",
        type: "",
      },
    };
  },
  created() {
    this.getShop();
    bus.$on("update", () => {
      this.getShop();
    });
  
  },
  methods: {
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.getShop();
    },
    // 处理size变化
    handleSizeChange(val) {
      // 当每页多少条发生变化时候，重新更新搜索条件size
      // 重新获取员工列表
      console.log(`每页 ${val} 条`);
      this.search.size = val;
      this.getShop();
    },
    // 处理current变化
    handleCurrentChange(val) {
      // 当当前页发生变化时候更新 搜索条件的当前页
      // 获取数据
      this.search.current = val;
      this.getShop();
    },
    getShop() {
      var search = {};
      // 如果是空的字段，不添加到搜索条件
      for (var k in this.search) {
        if (this.search[k]) {
          search[k] = this.search[k];
        }
      }
      GetShop(search).then((res) => {
        if (res.data.code === 0) {
          this.shop = res.data.data;
          this.pagnation = res.data.pagnation;
          this.pagnation.current = this.pagnation.current * 1;
          this.pagnation.size = this.pagnation.size * 1;
          console.log(this.shop,1111);
        }
      });
    },
    delShop(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DelShop(id).then((res) => {
            if (res && res.data && res.data.code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              // 删除成功，重新获取员工
              this.getShop();
            } else {
              this.$message({
                type: "success",
                message: res.data.msg || "删除失败",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    onSubmit() {
      console.log("submit!");
    },
  },
};
</script>
<style lang="scss" scoped>
.nav {
  display: flex;
  justify-content: space-between;
}
</style>
